package com.longrise.android.byjk.plugins.dealsituation.course.score;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.longrise.android.byjk.R;
import com.longrise.common.base.BaseActivity;
import com.longrise.android.byjk.event.CourseDetailEvent;
import com.longrise.common.utils.AppUtil;
import com.longrise.common.utils.DZZWTools;
import com.longrise.android.byjk.utils.DialogUtil;
import com.longrise.android.byjk.utils.Tracker_Google;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by godliness on 2017/8/15.
 * 课程评价
 */

public class ScoreActivity extends BaseActivity<ScorePresenter> implements
        ScoreView, View.OnClickListener, RatingBar.OnRatingBarChangeListener {

    public static final String TAG = "ScoreActivity";

    public static final String INTENT_EXTRA_PARAMS = "courseid";
    public static final String INTENT_RESULT_PARAMS = "result_params";

    private Toolbar mToolbar;

    /**
     * 课程部分
     */
    private ImageView mIvCourse;
    private TextView mTvCoruseTitle;
    private RatingBar mRb1;
    private TextView mTvTeacher;
    private TextView mTvPF;

    /**
     * 评价部分
     */
    private RatingBar mRb2;
    private RatingBar mRb3;
    private Button mBtnComment;

    private String mCourseId;

    private boolean mIshavemyscore;
    private int mDouBar1 = 0;
    private int mDouBar2 = 0;

    private boolean mScoreed;
    private TextView mTvTime;
    private RelativeLayout mRl_top;
    private TextView mTvpjlr;
    private RelativeLayout mRl_2;
    private RelativeLayout mRl_3;

    @Override
    public int getContentViewId(@Nullable Bundle saveState) {
        if (saveState != null) {
            onRestoreState(saveState);
        } else {
            getExtraData();
        }
        return R.layout.activity_score;
    }

    private void getExtraData() {
        Intent extraIntent = getIntent();
        mCourseId = extraIntent.getStringExtra(INTENT_EXTRA_PARAMS);
    }

    private void initTitle() {
        mToolbar = (Toolbar) findViewById(R.id.byjk_toolbar);
        RelativeLayout rightRl = (RelativeLayout) findViewById(R.id.bb_toolbar_right_rl_tv);
        rightRl.setVisibility(View.VISIBLE);
        mToolbar.setTitle("课程评价");
        setSupportActionBar(mToolbar);

        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void initView() {
        initTitle();
        mRl_top = (RelativeLayout) findViewById(R.id.courses_evaluate_rl);
        mTvpjlr = (TextView) findViewById(R.id.courses_evaluate_pjlr);
        mRl_2 = (RelativeLayout) findViewById(R.id.courses_evaluate_rl2);
        mRl_3 = (RelativeLayout) findViewById(R.id.courses_evaluate_rl3);

        mIvCourse = (ImageView) findViewById(R.id.courses_evaluate_iv);
        mTvCoruseTitle = (TextView) findViewById(R.id.courses_evaluate_tv1);
        mTvTeacher = (TextView) findViewById(R.id.courses_evaluate_tv_teacher);
        mTvTime = (TextView) findViewById(R.id.courses_evaluate_tv_time);
        mRb1 = (RatingBar) findViewById(R.id.courses_evaluate_rt1);
        mRb1.setMax(100);
        mTvPF = (TextView) findViewById(R.id.text_pjf);

        mRb2 = (RatingBar) findViewById(R.id.rating1);
        mRb3 = (RatingBar) findViewById(R.id.rating2);
        mBtnComment = (Button) findViewById(R.id.courses_evaluate_bt);
        regEvent(true);

        DZZWTools.showWaitDialog(this);
        mPresenter.toRequestPF(mCourseId);

        Tracker_Google.getInstance().addGoogleScreenName("课程评价");
    }

    private void regEvent(boolean isClick) {
        if (mBtnComment != null) {
            mBtnComment.setOnClickListener(isClick ? this : null);
        }
        if (mRb2 != null) {
            mRb2.setOnRatingBarChangeListener(isClick ? this : null);
        }
        if (mRb3 != null) {
            mRb3.setOnRatingBarChangeListener(isClick ? this : null);
        }
    }

    @Override
    public void setCourseInfor(String picUrl, String teacher, String time, String courseName, String avg) {
        if (isFinishing()) {
            return;
        }

        mRl_top.setVisibility(View.VISIBLE);
        mTvpjlr.setVisibility(View.VISIBLE);
        mRl_2.setVisibility(View.VISIBLE);
        mRl_3.setVisibility(View.VISIBLE);

        if (mIvCourse != null) {
            Glide.with(this)
                    .load(picUrl)
                    .placeholder(AppUtil.getResources().getDrawable(R.drawable.img03))
                    .error(AppUtil.getResources().getDrawable(R.drawable.img03))
                    .into(mIvCourse);
        }
        if (mTvTeacher != null) {
            mTvTeacher.setText(teacher);
        }
        if (mTvCoruseTitle != null) {
            mTvCoruseTitle.setText(courseName);
        }
        if (mTvPF != null) {
            mTvPF.setText(avg);
        }
        if (mTvTime != null) {
            mTvTime.setText(time);
        }
    }

    @Override
    public void setCommentBtnState(int state) {
        if (mBtnComment != null) {
            mBtnComment.setVisibility(state);
        }
    }

    @Override
    public void setRatingValue(boolean ishavemyscore, float avgValue, int rat1, int rat2) {
        if (mRb1 != null) {
            mRb1.setRating(avgValue);
        }
        if (mRb2 != null) {
            mRb2.setRating(rat1);
        }
        if (mRb3 != null) {
            mRb3.setRating(rat2);
        }
        this.mIshavemyscore = ishavemyscore;
        isIndicator(ishavemyscore);
    }

    @Override
    public void showCommentSuccessDialog() {
        if (isFinishing()) {
            return;
        }
        mScoreed = true;
        View view = View.inflate(this, R.layout.dialog_courses_evaluate, null);
        Dialog dialog = DialogUtil.getInstance().creatAlertDialog(this, view, 266, 195);
        TextView mTvDialog = (TextView) view.findViewById(R.id.dialog_courses_evaluate_bt);
        mTvDialog.setOnClickListener(this);
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                    dialog.dismiss();
                    onBack();
                }
                return false;
            }
        });
    }

    @Override
    public boolean actFinished() {
        return isFinishing();
    }

    private void onBack() {
        if (mScoreed) {
            CourseDetailEvent event = new CourseDetailEvent();
            event.setRefreshScore(true);
            EventBus.getDefault().post(event);
        }
        finish();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(INTENT_EXTRA_PARAMS, mCourseId);
    }

    private void onRestoreState(Bundle saveState) {
        if (saveState != null) {
            mCourseId = saveState.getString(INTENT_EXTRA_PARAMS);
        }
    }

    @Override
    public void showToast(String msg) {
        super.showToast(msg);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.courses_evaluate_bt:
                toComment();
                break;
            case R.id.dialog_courses_evaluate_bt:
                DialogUtil.getInstance().dismiss();
                onBack();
                break;

            default:
                break;
        }
    }

    @Override
    public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
        switch (ratingBar.getId()) {
            case R.id.rating1:
                mDouBar1 = (int) rating;
                break;

            case R.id.rating2:
                mDouBar2 = (int) rating;
                break;

            default:
                break;
        }
    }

    private void toComment() {
        if (mIshavemyscore) {
            DZZWTools.showToast(mContext, "您已评价过该课程", 1000 * 2);
            return;
        }
        if (mDouBar1 == 0) {
            DZZWTools.showToast(mContext, "请对课程实用性进行评价", 1000 * 2);
            return;
        }
        if (mDouBar2 == 0) {
            DZZWTools.showToast(mContext, "请对课程美观度进行评价", 1000 * 2);
            return;
        }
        DZZWTools.showWaitDialog(this);
        mPresenter.toCommentRequest(mCourseId, mDouBar1, mDouBar2);
    }

    private void isIndicator(boolean bool) {
        if (bool) {
            mRb2.setIsIndicator(true);
            mRb3.setIsIndicator(true);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
